//
// Created by admin on 2024/8/16.
//

#include "Control.h"
#include "config.h"
#include "Resource/MusicCode.h"

#include <I2C_BM8563.h>
#include <Adafruit_NeoPixel.h>
#include <WiFi.h>

Adafruit_NeoPixel pixels(2, WLED_PIN, NEO_GRB + NEO_KHZ800);
I2C_BM8563 rtc(I2C_BM8563_DEFAULT_ADDRESS, Wire);

I2C_BM8563_DateTypeDef dateStruct;
I2C_BM8563_TimeTypeDef timeStruct;

/*
 * @brief 初始化一些外设，WS2812,时钟模块，蜂鸣器，LED灯，BAT电压检测
 */
void Control::init() {

    pixels.begin();
    rtc.begin();

//    //     Set RTC Date
//    I2C_BM8563_DateTypeDef dateStruct;
//    dateStruct.month = 10;
//    dateStruct.date = 1;
//    dateStruct.year = 2024;
//    rtc.setDate(&dateStruct);
//
//    // Set RTC Time
//    I2C_BM8563_TimeTypeDef timeStruct;
//    timeStruct.hours   = 23;
//    timeStruct.minutes = 8;
//    timeStruct.seconds = 0;
//    rtc.setTime(&timeStruct);

    ledcSetup(LED_PWM_CHANNEL, 1000, 8);
    ledcAttachPin(LED_PIN, LED_PWM_CHANNEL);

    ledcSetup(BEE_PWM_CHANNEL,5000,10);
    ledcAttachPin(BEE_PIN, BEE_PWM_CHANNEL);

    pinMode(BAT_PIN, INPUT);
    pinMode(EN_PIN,OUTPUT);
    digitalWrite(EN_PIN,HIGH);

}

// 设置WS2812每个灯的颜色
void Control::setRGB(int id,int r, int g, int b){
    pixels.setPixelColor(id, pixels.Color(r, g, b));
    pixels.show();
}

void Control::CloseRGB(){
    pixels.clear();
    pixels.show();
}

// 设置WS2812亮度
void Control::setWBrightness(float brightness) {
    pixels.setBrightness(int(brightness*255));
}

// 设置手电筒亮度
void Control::setBrightness(float brightness) {
    ledcWrite(LED_PWM_CHANNEL, int(brightness*255));
}

//播放提示音
bool Control::Audio_play(const char *name) {
    bool play = false;
    for (int i = 0; i < sizeof(MusicList) / sizeof(MusicList[0]); i++)
    {
        if (strcmp(name, MusicList[i].name) == 0)
        {
            for(int j=0;j<MusicList[i].musicsize;j++){
                ledcWriteNote(BEE_PWM_CHANNEL,MusicList[i].music[j].note,MusicList[i].music[j].octave);
                delay(MusicList[i].music[j].duration);
            }
            play= true;
            ledcWrite(BEE_PWM_CHANNEL,0);
            break;
        }
    }
    return play;
}

void Control::getTime() {
    rtc.getDate(&dateStruct);
    rtc.getTime(&timeStruct);
}

bool Control::updateTime() {
    if(WiFi.status() != WL_CONNECTED){
        return false;
    }

    configTime(utcOffsetInSeconds, 0, ntpServer);//从网络时间服务器上获取并设置时间
    struct tm timeInfo;

    int temp = 0;
    while (!getLocalTime(&timeInfo) && temp<10) {
        delay(1000);
        Serial.println("Waiting for time sync...");
        temp++;
    }
    if (temp >= 10) {
        Serial.println("Failed to get time from network.");
        return false;
    } else {
        // Set RTC time
        timeStruct.hours   = timeInfo.tm_hour;
        timeStruct.minutes = timeInfo.tm_min;
        timeStruct.seconds = timeInfo.tm_sec;
        rtc.setTime(&timeStruct);

        // Set RTC Date
        dateStruct.weekDay = timeInfo.tm_wday;
        dateStruct.month   = timeInfo.tm_mon + 1;
        dateStruct.date    = timeInfo.tm_mday;
        dateStruct.year    = timeInfo.tm_year + 1900;
        rtc.setDate(&dateStruct);
    }
    return true;
}